'use strict'
const path = require('path')
const settings = require('./src/settings.js')
const HtmlWebpackPlugin = require('html-webpack-plugin')
function resolve(dir) {
  return path.join(__dirname, dir)
}
const Timestamp = new Date().getTime()

const assetsCDN = {
  'vue': 'Vue',
  'vuex': 'Vuex',
  'vue-router': 'VueRouter',
  'axios': 'axios',
  'element-ui': 'ELEMENT'
}

const name = settings.title || 'vue'
module.exports = {
  publicPath: '',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: '8000',
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/drp': {
        target: 'https://fengyanginfo.com/',
        // target: 'http://192.168.10.20/',
        ws: true,
        changeOrigin: true,
        secure: false,
        pathRewrite: {// 如果接口本身没有/drp需要通过pathRewrite来重写了地址
          '^/drp': '/drp'
        }
      },
      '/gl': {
        target: 'https://fengyanginfo.com/',
        // target: 'http://192.168.10.20/',
        ws: true,
        changeOrigin: true,
        secure: false,
        pathRewrite: {// 如果接口本身没有/drp需要通过pathRewrite来重写了地址
          '^/gl': '/gl'
        }
      }
    }
  },
  configureWebpack: {
    name,
    externals: process.env.NODE_ENV === 'development' ? {} : assetsCDN,
    plugins: [
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: process.env.NODE_ENV === 'development' ? 'public/index.dev.html' : 'public/index.html',
        inject: true
      })
    ],
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    output: {
      filename: `static/js/[name].${Timestamp}.js`,
      chunkFilename: `static/js/[name].${Timestamp}.js` // 设置js文件在static/js下面，并自动忽略上面的assetsDir
    },
    performance: {
      hints: 'warning',
      // 入口起点的最大体积
      maxEntrypointSize: 50000000,
      // 生成文件的最大体积
      maxAssetSize: 30000000,
      // 只给出 js 文件的性能提示
      assetFilter: (assetFilename) => {
        return assetFilename.endsWith('.js')
      }
    }
  },
  chainWebpack(config) {
    config.plugins.delete('preload').delete('prefetch') /** 删除懒加载模块的 prefetch preload，降低带宽压力(使用在移动端) */
    // config.optimization.minimize(true)
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module.rule('icons').test(/\.svg$/).include.add(resolve('src/icons')).end().use('svg-sprite-loader').loader('svg-sprite-loader').options({
      symbolId: 'icon-[name]'
    }).end()
    config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
      options.compilerOptions.preserveWhitespace = true
      return options
    }).end()
    config.when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map'))
  }
}
